package com.jingmi.jingmiliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.jingmi.common.Validated.saveValidatedGroup;
import com.jingmi.common.Validated.updateIsShowValidateGroup;
import com.jingmi.common.Validated.updateValidatedGroup;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jingmi.jingmiliving.commodity.entity.BrandEntity;
import com.jingmi.jingmiliving.commodity.service.BrandService;
import com.jingmi.common.utils.PageUtils;
import com.jingmi.common.utils.R;



/**
 * 家居品牌
 *
 * @author jingmi
 * @email 2771368705@qq.com
 * @date 2023-10-06 15:20:58
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        System.out.println("==================================");
        System.out.println("page====>"+page.getList());
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:brand:save")
    public R save(@Validated({saveValidatedGroup.class}) @RequestBody BrandEntity brand  /*,BindingResult result*/){

        /*Map<String, String> map = new HashMap<>();
        if (result.hasErrors()){
            result.getFieldErrors().forEach((item) -> {
                System.out.println("item.getField()==>"+item.getField()+
                        "item.getDefaultMessage()==>"+item.getDefaultMessage());
                //map.put(item.getField(),item.getDefaultMessage());
            });
            return R.error(400,"品牌表单数据不合法").put("data",map);
        }else {*/
            brandService.save(brand);
            return R.ok();
        //}


    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:brand:update")
    public R update(@Validated({updateValidatedGroup.class})@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }
    @RequestMapping("/update/isshow")
    //@RequiresPermissions("commodity:brand:update")
    public R updateIsshow(@Validated({updateIsShowValidateGroup.class})@RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
